Accessing HTML DOM elements from javascript using `.childNodes`

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2012-04-10T23:16:54Z Indexed on 2012/04/10 23:29 UTC
Read the original article Hit count: 177

Filed under:
|

I'm wondering about the .childNodes property, I have the code below, and for some reason I get 18 children, while 6 are HTMLInputElements as expected, and the rest are undefined. What is this about? Is there an efficient way to iterate over the input elements?

<html>
  <head>
    <script>
    window.onload = function(e){
        form = document.getElementById('myForm');
        alert(form.childNodes.length);
        for(i=0; i<form.childNodes.length; i++){
            alert(form[i]);
        }
    }
    </script>
  </head>
  <body>
<form id='myForm' action="haha" method="post">
Name: <input type="text" id="fnameAdd" name="name" /><br />
Phone1: <input type="text" id="phone1Add" name="phone1" /><br />
Phone2: <input type="text" id="phone2Add" name="phone2" /><br />
E-Mail: <input type="text" id="emailAdd" name="email" /><br />
Address: <input type="text" id="addressAdd" name="address" /><br />
<input type="submit" value="Save" />
</body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html-dom